﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using CLibArtreve.dal;
using CLibArtreve.models;
using System.Data;

namespace CLibArtreve.bll
{
    public class RatingBLL
    {
        #region VARIABLES
        private string _connectionString;        
        private RatingDAL _adapter;
        #endregion

        #region PROPERTIES
        public string ConnectionString
        {
            get {
                if (_connectionString == null)
                    _connectionString = ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
                return _connectionString; 
            }
            set { _connectionString = value; }
        }
        public RatingDAL Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new RatingDAL(ConnectionString);
                return _adapter;
            }
        }
        #endregion

        #region CONSTRUCTOR
        public RatingBLL(string connectionString)
        {
            ConnectionString = connectionString;
        }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "BUSINESS LOGIC LAYER: " + this.GetType().ToString();
        }
        #endregion

        #region GET METHODS
        public IList<Rating> GetAllRating()
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAllRatings();
                //MAKE A LIST
                IList<Rating> list = new List<Rating>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateRatingFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IList<Rating> GetRatingByArtId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetRatingByArtId(id);
                //MAKE A LIST
                IList<Rating> list = new List<Rating>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateRatingFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IList<Rating> GetRatingByUserId(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetRatingByUserId(id);
                //MAKE A LIST
                IList<Rating> list = new List<Rating>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateRatingFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Rating GetRatingByArtIdAndUserId(Int64? userId, Int64 artId)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetRatingByArtIdAndUserId(userId, artId);

                //IF DATASET EXISTS EN IT HAS ROWS RETURN THE ROW
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateRatingFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        

        #endregion

        #region INSERT METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int InsertRating(Rating rating)
        {
            try
            {
                return Adapter.InsertRating(rating.ArtId, rating.Stars, rating.UserId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UPDATE METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int UpdateArtByArtIdAndUserId(Rating rating)
        {
            try
            {
                return Adapter.UpdateArtByArtIdAndUserId(rating.ArtId, rating.Stars, rating.UserId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UTILITIES
        private Rating GenerateRatingFromDataRow(DataRow row)
        {
            Rating obj = new Rating();
            obj.ArtId = Convert.ToInt64(row["rating_art_id"]);
            obj.Stars = (int)row["rating_stars"];
            obj.UserId = Convert.ToInt64(row["users_user_id"]);
            
            return obj;
        }
        #endregion
    }
}
